/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package trab_sd;

import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.net.*;

/**
 *
 * @author moacirnq
 */
public class OfflineUserMsg implements Runnable{
	private Socket connection;
	private UserID offUser, dst;
	private boolean cont = true;
	
	public OfflineUserMsg( UserID offUser, UserID dst ) {
		this.offUser = offUser;
		this.dst = dst;

		if (dst.equals(offUser)) {
			cont = false;
				return;
		}
		
		//tenta criar as conexoes
		try {
			connection = new Socket( dst.getName(), dst.getPort() );
			connection.setSoTimeout(50000);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		try {
			if (cont == false) return;
			//cria stream de conexao
			ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
			//envia msg
			out.writeUTF("OFFLINE_USER");
			out.flush();
			out.writeObject(offUser);
			out.flush();
			out.close();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			try {
				if (cont == true)
				connection.close();
			}
			catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
